const { model, sequelize } = require("../../models")
const uuid = require("uuid")
async function insertFile({ userId, md5 = "", oldFileName, fileName, createTime }) {
    var data = await model.filelist.create({
        id: uuid.v4(),
        userId, md5, oldFileName, fileName, createTime, active: 1
    })
    return JSON.parse(JSON.stringify(data))
}
async function getFileById(id) {
    var data = await model.filelist.findOne({
        where: {
            id,
            active: 1
        }
    })
    return JSON.parse(JSON.stringify(data));
}
async function fileList({ id, oldFileName = "", pageNum, pageSize }) {
    var total = (await sequelize.query('SELECT COUNT(*) total FROM filelist f where f.userId=? AND active=1 AND oldFileName like ?', {
        replacements: [id, '%' + oldFileName + '%']
    }))[0][0]
    var data = (await sequelize.query('SELECT * FROM filelist f where f.userId=? AND active=1 AND oldFileName like ? ORDER BY f.createTime desc LIMIT ? OFFSET ?', {
        replacements: [id, '%' + oldFileName + '%', pageSize, (pageNum - 1) * pageSize]
    }))[0]
    return { fileListData: JSON.parse(JSON.stringify(data)), total: JSON.parse(JSON.stringify(total)).total };
}
async function fileByMd5(md5) {
    var data = await model.filelist.findOne({
        where: {
            md5,
            active: 1
        }
    })
    return JSON.parse(JSON.stringify(data));
}
module.exports = {
    insertFile,
    getFileById,
    fileList,
    fileByMd5
}